package com.aji.wineserver.service.impl;

import com.aji.winecommon.constant.IdentConstant;
import com.aji.winecommon.result.PageResult;
import com.aji.winecommon.utills.CsvUtills;
import com.aji.winecommon.utills.SpectrumUtill;
import com.aji.winepojo.dto.WineDto;
import com.aji.winepojo.entity.ClassData;
import com.aji.winepojo.entity.IdentData;
import com.aji.winepojo.entity.Wine;
import com.aji.winepojo.entity.WineAdd;
import com.aji.winepojo.vo.WineVo;
import com.aji.winepojo.vo.WineVoPlus;
import com.aji.wineserver.config.FileEnvConfig;
import com.aji.wineserver.mapper.ClassMapper;
import com.aji.wineserver.mapper.WineMapper;
import com.aji.wineserver.service.WineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class WineServiceImpl extends ServiceImpl<WineMapper, Wine>  implements WineService {
    @Autowired
    private WineMapper wineMapper;
    @Autowired
    private FileEnvConfig fileEnvConfig;
    @Autowired
    private ClassMapper classMapper;
    
    @Override
    public List<WineVo> selectCon(WineDto wineDto) {
        return wineMapper.pageQuery(wineDto);
    }

    @Override
    public PageResult pageQuery(WineDto wineDto, int page1, int pageSize) throws IOException {
        log.info("wine模糊查询service:{},{},{}",wineDto.toString(),page1,pageSize);
        PageHelper.startPage(page1,pageSize);
        List<WineVo> elementVoList = wineMapper.pageQuery(wineDto);
        Page<WineVo> page = (Page<WineVo>) elementVoList;
        long totol=page.getTotal();
        List<WineVo>records=page.getResult();
        List<WineVoPlus> wineVoPlusList = new ArrayList<>();
        for (WineVo wineVo:records) {
            WineVoPlus wineVoPlus = new WineVoPlus();
            BeanUtils.copyProperties(wineVo,wineVoPlus);
            wineVoPlus.setSpectrumList(SpectrumUtill.strToList(wineVo.getSpectrum()));
            wineVoPlus.setSpectrumX(SpectrumUtill.getX(fileEnvConfig.getDataPath()+fileEnvConfig.getDemoFile()));
            wineVoPlusList.add(wineVoPlus);
        }
        return new PageResult(totol,wineVoPlusList);
    }

    @Override
    public WineVo selectById(int id) {
        return wineMapper.selectById(id);
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        log.info("wine删除service:{}",ids.toString());
        wineMapper.deleteByIds(ids);
    }


    @Override
    public Integer add(MultipartFile multipartFile) {
        List<WineAdd> wineAdds = CsvUtills.csvToList(multipartFile);
        int i = 0;
        for(WineAdd wineAdd :wineAdds){
            i++;
            if(wineAdd.getWineClassName() != null){
                int classId = classMapper.getClassByName(wineAdd.getWineClassName());
                wineAdd.setWineClass(classId);
                wineAdd.setWineIdent(IdentConstant.PersonIdentified);
            }
            else {
                wineAdd.setWineIdent(IdentConstant.UnIdentified);
            }
            wineAdd.setCreateTime(LocalDateTime.now());
        }
        wineMapper.add(wineAdds);
        return i;
    }

    @Override
    public void upDate(int id, MultipartFile multipartFile, Integer idenId, String pictureUrl) {
        Wine wine;
        if(multipartFile == null || multipartFile.isEmpty()){
            wine = Wine.builder()
                    .wineId(id)
                    .wineIdent(idenId)
                    .pictureUrl(pictureUrl)
                    .changeTime(LocalDateTime.now())
                    .build();
            wineMapper.upDate(wine);
            return;
        }
        List<WineAdd> wineAdds = CsvUtills.csvToList(multipartFile);
        WineAdd wineAdd = wineAdds.get(0);
        int classId = classMapper.getClassByName(wineAdd.getWineClassName());
        wine = Wine.builder()
                .wineId(id)
                .wineName(wineAdd.getWineName())
                .spectrum(wineAdd.getSpectrum())
                .wineClass(classId)
                .wineIdent(idenId)
                .agingCondition(wineAdd.getAgingCondition())
                .pictureUrl(pictureUrl)
                .changeTime(LocalDateTime.now())
                .build();

      wineMapper.upDate(wine);
    }

    @Override
    public List<ClassData> selectClassData() {
        return wineMapper.selectClassData();
    }

    @Override
    public List<IdentData> selectIdentData() {
        return wineMapper.selectIdentData();
    }

    @Override
    public List<WineVo> listByIds(List<Integer> ids) {
        return wineMapper.listByIds(ids);
    }

    @Override
    public void changeIdentfication(List<Integer> ids) {
        wineMapper.changeIdentfication(ids);
    }
}
